<template>
  <div>
    <!-- 标题 -->
    <van-nav-bar
      title="新增联系人"
      left-arrow
      @click-left="onClickLeft"
    />
    <!-- 新建联系人 -->
    <van-address-edit
      v-if="this.edit==''"
      :area-list="areaList"
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @change-detail="onChangeDetail"
    />
    <!-- 编辑联系人 -->
    <van-address-edit
      v-else
      :area-list="areaList"
      :address-info="edit"
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onedit"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
export default {
  name: "addressedit",
  data() {
    return {
      areaList,
      searchResult: [],
      contactFlag: 0,
      edit: [],
    };
  },
  methods: {
    onClickLeft() {
      window.history.go(-1);
    },
    // 保存
    onSave(index) {
      console.log(index);
      let address =
        index.province + index.city + index.county + index.addressDetail;
      let tel = index.tel;
      let name = index.name;
      if (index.isDefault == true) {
        this.contactFlag = 1;
      }
      this.axios
        .post("apis/kuan_api/address.php", {
          code: 2,
          userid: this.$store.state.user.user_id,
          name: name,
          mobile: tel,
          contactFlag: this.contactFlag,
          address: address,
        })
        .then((res) => {
          this.$toast.success("添加成功");
          this.$router.push("/address");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 编辑保存
    onedit(index) {
      let address =
        index.province + index.city + index.county + index.addressDetail;
      let tel = index.tel;
      let name = index.name;
      if (index.isDefault == true) {
        this.contactFlag = 1;
      }
      this.axios
        .post("apis/kuan_api/address.php", {
          code: 3,
          addressid: index.id,
          userid: this.$store.state.user.user_id,
          name: name,
          mobile: tel,
          contactFlag: this.contactFlag,
          address: address,
        })
        .then((res) => {
          this.$toast.success("修改成功");
          this.$router.push("/address");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 删除
    onDelete() {
      this.axios
        .post("apis/kuan_api/address.php", {
          code: 4,
          addressid: this.edit.id,
        })
        .then((res) => {
          this.$toast.success("删除成功");
          this.$router.push("/address");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    onChangeDetail() {},
  },
  created() {
    console.log(this.$route.query.edit);
    console.log(this.edit);
    if (this.$route.query.edit != undefined) {
      this.edit = this.$route.query.edit;
    }
  },
};
</script>

<style scoped>
</style>